//---------------------------------------------------------------------------

#ifndef CIIServerSocketH
#define CIIServerSocketH

#include "DebugWinU.h"
#include "CIISCommon.h"
#include <scktcomp.hpp>

//---------------------------------------------------------------------------

class TCIIServerSocket : public TObject
{
private:
  TDebugWin *DW;
  TList *MessageList, *ResponseList;
  TServerSocket *ServSocket;
  TCustomWinSocket *CWSocket;
  TTimer *TimerSRead;
	MessInQueueFP MInQ;
	bool FClientLoggedIn;
	CIIUserLevel FUserLevel;
	bool FPrjSelected, FPrjSelOnline;

  unsigned char SocketInBuf[SocketInBufSize];
  unsigned char SocketOutBuf[SocketOutBufSize];
  unsigned char SocketInputQueue[SocketInputQueueSize];
  int SocketInputIdx, PortNo;
  Word ResponseCount;
  bool ContSocketRead, FClientConnected;

  void __fastcall Debug(String Message);
  void __fastcall AddSocketInputQueue(char* Buf, int N);
  
  bool __fastcall ParseMessages();
  //bool __fastcall ParseCommands();

protected:

public:
        __fastcall TCIIServerSocket(MessInQueueFP SetMInQ, int SetPortNo, TDebugWin * SetDebugWin);
        __fastcall ~TCIIServerSocket();
    void __fastcall SetDebugWin(TDebugWin * SetDebugWin);
    void __fastcall TimerSReadEvent(TObject * Sender);
    void __fastcall SocketAccept(TObject * Sender, TCustomWinSocket * Socket);
    void __fastcall ClientConnect(TObject * Sender, TCustomWinSocket * Socket);
		void __fastcall ClientDisconnect(TObject * Sender, TCustomWinSocket * Socket);
		void __fastcall ClientOnError(TObject *Sender, TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode);
    void __fastcall ClientRead(TObject * Sender, TCustomWinSocket * Socket);
    TCamurPacket* __fastcall GetMessage();
		void __fastcall PutMessage( TCamurPacket * o );
		void __fastcall CancelSendMessage();
    void __fastcall SendMessage();
		void __fastcall ConnectClient();
		void __fastcall DisconnectClient();

__published:
		__property bool ClientConnected  = { read=FClientConnected };
		__property bool ClientLoggedIn = { read=FClientLoggedIn, write=FClientLoggedIn };
		__property CIIUserLevel UserLevel = { read=FUserLevel, write=FUserLevel };
		__property bool PrjSelected = { read=FPrjSelected, write=FPrjSelected };
		__property bool PrjSelOnline = { read=FPrjSelOnline, write=FPrjSelOnline };

};







#endif
